#include <config.h>
#include <ysip2.h>


void usage() {
	cout << " ysip2 version 2.0 " << endl;
	cout << "  ./test -i <ip> | -r <region_id> | -p <region_id> | -c <region_id> | -h" <<endl;
}


void test_ysip_get_childs(int64_t region_id)
{
	vector<YSIP2_REGION_INFO> vec;
	if(ysipGetRegionChilds(region_id, vec)) {
		cout << "The result is: " << endl;
		for(vector<YSIP2_REGION_INFO>::iterator iter = vec.begin(); iter != vec.end(); iter++) {
			cout << * iter << endl;
		}
	} else {
		cout << "ERROR: get region id " << region_id << " 's child error " << endl;
	}
}
void test_ysip_get_parents(int64_t region_id)
{
	vector<YSIP2_REGION_INFO> vec;
	if(ysipGetRegionParents(region_id, vec, true, 2)) {
		cout << "The result is: " << endl;
		for(vector<YSIP2_REGION_INFO>::iterator iter = vec.begin(); iter != vec.end(); iter++) {
			cout << * iter << endl;
		}
	} else {
		cout << "ERROR: get region id " << region_id << " 's parents error " << endl;
	}
}
void test_ysip_get_ip_info(string ip)
{
	YSIP2_REGION_INFO r;

	if(ysipGetIpInfo(ip, r)) {
		cout << "The result is: " << endl;
		cout << r << endl;
	} else {
		cout << "ERROR: Get ip: " << ip << " error" << endl;
	}
}

void test_ysip_get_ip_info_ex(string ip)
{

	YSIP2_REGION_INFO_EX r;

	if(ysipGetIpInfo(ip, r)) {
		cout << "The result is: " << endl;
		cout << r << endl;
	} else {
		cout << "ERROR: Get ip: " << ip << " error" << endl;
	}
}
void test_load(string ip , int count) {
	if(count <= 0) {
		count = 5000000;
	}

	YSIP2_REGION_INFO r;
	int succ = 0;
	clock_t begin = clock();
	for(int i = 0; i < count ; i ++) {
		if(ysipGetIpInfo(ip,  r)) {
			succ ++ ;
		}	else {
			break;
		}
	}
	clock_t end = clock();
	if(end - begin > 1) {
		cout << "begin is " << begin << endl;
		cout << "end is " << end << endl;
		cout << "count is " << count << endl;
		cout << "avg is " << count/((end - begin)/CLOCKS_PER_SEC) << endl;
		cout << "succ is " << succ << endl;
	} else {
		cout << "We have not the ip " << ip << endl;
	}
}
void test_ysip_get_region_info(int64_t region_id)
{

	YSIP2_REGION_INFO r;
	if(ysipGetRegionInfo(region_id, r)) {
		cout << "The result is: " << endl;
		cout << r << endl;
	} else {
		cout << "ERROR: Get region id " << region_id << " error " << endl;
	}
}

int main(int argc, char ** argv)
{
//	long cbegin = clock();
	ysipInit("/home/z");
//	long cend = clock();

	int i = 1;
	char o;
	string ip;
	YSIP2_REGION_INFO r;
	while( i < argc) {
		if(argv[i][0] == '-' && strlen(argv[i]) >= 2) {
			o = argv[i][1];
			switch(o) {
				case 'i':
					if(i + 1 < argc) {
						test_ysip_get_ip_info(argv[i+1]);
						i++;
					} else {
						cout << "ERROR: You have not give me the ip " << endl;
					}
					break;
				case 'h':
					usage();
					break;
				case 'r':
					if(i + 1 < argc) {
						test_ysip_get_region_info(atoi(argv[i+1]));
						i++;
					} else {
						cout << "ERROR: You have not give me the region id " << endl;
					}
					break;
				case 'd':
					if(i + 1 < argc) {
						test_ysip_get_ip_info_ex(argv[i+1]);
						i++;
					} else {
						cout << "ERROR: You have not give me the ip string " << endl;
					}
					break;
				case 'p':
					if(i+1 < argc) {
						test_ysip_get_parents(atoi(argv[i+1]));
						i++;
					} else {
						cout << "ERROR: You have not give me the region id " << endl;
					}
					break;
				case 'c':
					if(i+1 < argc) {
						test_ysip_get_childs(atoi(argv[i+1]));
						i++;
					} else {
						cout << "ERROR: You have not give me the region id " << endl;
					}
					break;
				case 'l':
					if(i+2 < argc) {
						test_load(argv[i+1], atoi(argv[i+2]));
						i++;
					} else {
						cout << "ERROR: please give me the ip and load number" << endl;
					}
					break;
			}
		}
		i ++;
	}

	ysipDestory();
	return 0;
}
